﻿using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using OSharp.Core.Options;
using OSharp.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Starzero.Framework.Utility.Data
{
    public class DapperService
    {
        public static IDbConnection GetConnection()
        {
            //IConfiguration configuration = Singleton<IConfiguration>.Instance;
            //IConfigurationSection section = configuration.GetSection("OSharp:DbContexts");
            //IDictionary<string, OSharpDbContextOptions> dict = section.Get<Dictionary<string, OSharpDbContextOptions>>();
            //if (dict == null || dict.Count == 0)
            //{
            //    return new SqlConnection(configuration["ConnectionStrings:DefaultDbContext"]);
            //}
            //else
            //{
            //    KeyValuePair<string, OSharpDbContextOptions> kvp = dict.FirstOrDefault();
            //    if (dict[kvp.Key].DatabaseType == OSharp.Entity.DatabaseType.SqlServer)
            //        return new SqlConnection(configuration["OSharp:DbContexts:" + kvp.Key + ":ConnectionString"]);
            //    else if (dict[kvp.Key].DatabaseType == OSharp.Entity.DatabaseType.MySql)
            //        return new MySqlConnection(configuration["OSharp:DbContexts:" + kvp.Key + ":ConnectionString"]);
            //    else
            //        throw new Exception("未知的数据库类型");
            //}
            return MySqlConnection();

        }

        public static string GetConnectionStr()
        {
            IConfiguration configuration = Singleton<IConfiguration>.Instance;
            return configuration["OSharp:DbContexts:Default:ConnectionString"];
        }


        public static SqlConnection SqlServerConnection()
        {
            IConfiguration configuration = Singleton<IConfiguration>.Instance;
            return new SqlConnection(configuration["OSharp:DbContexts:Default:ConnectionString"]);
        }

        public static MySqlConnection MySqlConnection()
        {
            IConfiguration configuration = Singleton<IConfiguration>.Instance;
            return new MySqlConnection(configuration["OSharp:DbContexts:Default:ConnectionString"]);
        }
    }
}

